| Conditions | 21 |
| Paths | 578 |
| Total Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like parseUrl.js ➔ processURL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort) { |
||
| 2 | if (URL === null || URL === "") { |
||
| 3 | return URL; |
||
| 4 | } |
||
| 5 | |||
| 6 | var parser = document.createElement('a'); |
||
| 7 | parser.href = URL; |
||
| 8 | |||
| 9 | |||
| 10 | var protocol = parser.protocol; |
||
| 11 | var host = parser.hostname; |
||
| 12 | var path = parser.pathname; |
||
| 13 | var port = parser.port; |
||
| 14 | if (host === null || host === "") { |
||
| 15 | return URL; |
||
| 16 | } |
||
| 17 | |||
| 18 | var splittedURL = host.split("."); |
||
| 19 | var isIP = false; |
||
| 20 | if (splittedURL.length === 4) { |
||
| 21 | isIP = true; |
||
| 22 | for (var i = 0; i < splittedURL.length; i++) { |
||
| 23 | if (isNaN(splittedURL[i]) || splittedURL[i] < 0 || splittedURL[i] > 255) { |
||
| 24 | isIP = false; |
||
| 25 | break; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | var baseHost = null; |
||
| 30 | if (isIP) { |
||
| 31 | baseHost = host; |
||
| 32 | } |
||
| 33 | else { |
||
| 34 | var tld = parse_host(host); |
||
| 35 | if(tld) { |
||
| 36 | baseHost = tld.domain; |
||
| 37 | } |
||
| 38 | } |
||
| 39 | var returnURL = ""; |
||
| 40 | if (!ignoreProtocol) { |
||
| 41 | returnURL += protocol + "//"; |
||
| 42 | } |
||
| 43 | |||
| 44 | if (!ignoreSubdomain) { |
||
| 45 | returnURL += host; |
||
| 46 | } |
||
| 47 | else { |
||
| 48 | returnURL += baseHost;//return the hostname and the tld of the website if ignoreSubdomain is check |
||
| 49 | } |
||
| 50 | |||
| 51 | if (ignorePort) { |
||
| 52 | if (port) { |
||
| 53 | returnURL = returnURL.replace(':' + port, ''); |
||
| 54 | } |
||
| 55 | } else { |
||
| 56 | if (port) { |
||
| 57 | returnURL += ':' + port; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if (!ignorePath && path !== null && path) { |
||
| 62 | returnURL += path; |
||
| 63 | } |
||
| 64 | if (returnURL.slice(-1) === "/") { |
||
| 65 | returnURL = returnURL.slice(0, -1); |
||
| 66 | } |
||
| 67 | return returnURL; |
||
| 68 | } |
||
| 69 |